accountingIntegrationDashboardApp.controller('baseMaintenanceCtrl', ['$scope', '$http', '$log', 'serverDataService', 'commonValidationService', function ($scope, $http, $log, serverDataService, commonValidationService){ 

    //general variables
    $scope.setChildScope = function (childScope) {
        $scope.childScope = childScope;
        //pagination variables centralized definition
        $scope.childScope.currentPage = 1;
        $scope.childScope.pageSizes = [5, 10, 15, 20, 25, 30];
        $scope.childScope.pageSize = 5;
        $scope.childScope.numPages = 4;
        $scope.childScope.totalPages = 0;
        $scope.childScope.totalItems = 0;
        $scope.childScope.maxSize = 8;
        //other commons variables
        $scope.childScope.deletingEntryInfo = '';
    };

    //pagination info and functions
    $scope.getPagedUpperLimit = function() {
        var result = $scope.childScope.currentPage * $scope.childScope.pageSize;
        if (($scope.childScope.currentPage * $scope.childScope.pageSize) > $scope.childScope.totalItems) {
            result = $scope.childScope.totalItems;
        }
        return result;
    };

    $scope.setPage = function (pageNo) {
        $scope.childScope.currentPage = pageNo;
    };

    //watch variable currentPage on change, update grid
    $scope.$watch('childScope.currentPage', function (newVal, oldVal) {
        if (newVal !== oldVal) {
            $scope.renderRecordsFromServer();
        }
    }, true);

    $scope.$watch('childScope.pageSize', function (newVal, oldVal) {
        if (newVal !== oldVal) {
            $scope.renderRecordsFromServer();
        }
    }, true);

    $scope.renderRecordsFromServer = function() {
        return serverDataService.getEntityListByPage($scope.childScope.entityName, true, ($scope.childScope.currentPage - 1), $scope.childScope.pageSize)
                .then(function (data){
                    $scope.childScope.gridRecords = data.gridRecords;
                    $scope.childScope.totalItems = data.totalRecords;
                });
    };

    $scope.saveOrupdateEntity = function(entity, id) {
        $scope.cleanResult();
        return serverDataService.saveOrupdateAndRenderEntity($scope.childScope, entity, id);
    };

    $scope.grabarTransaccion = function(transaccion) {
        return serverDataService.grabarTransaccion($scope.childScope, transaccion);
    };

    $scope.actualizarRegistro = function(registro) {
        return serverDataService.actualizarRegistro($scope.childScope, registro);
    };

    $scope.cancelSaving = function(index, id){
        $scope.cleanResult();
        if(!id){
            $scope.childScope.gridRecords.splice(index, 1);
        }
    };

    $scope.validateNotEmpty = function (columnName, data){
        msg = commonValidationService.checkNotEmpty(columnName, data);
        if (msg) {return msg;}
    };

    $scope.cleanResult = function (){
        $scope.childScope.errorResponse=null;
        $scope.childScope.successResponse=null;
    };

    $scope.recargar = function (){
        location.reload(true);
    };

}]);

accountingIntegrationDashboardApp.directive("jpdatepicker", function () {
    return {
        restrict: 'A',
        require : 'ngModel',
        link : function (scope, element, attrs, ngModelCtrl) {
          $(function(){
            element.datepicker({
              dateFormat:'dd-mm-yy',
              onSelect:function (date) {

                scope.$apply(function () {
                  ngModelCtrl.$setViewValue(date);
                });
              }
            });
          });
        }
    }
   });